package second.group.bering.parking_system

import second.group.bering.parking_system.model.Car;
/**
 *
 * Park
 *
 * @author Burgess Li, Thomas Chen
 * @date 8/23/2018 2:39 PM
 * @version 1.0
 *
 */
class Park {

    String id
    Integer size = 0
    Map<String, Car> cars = [:]

    /**
     * Task #1
     * @param car
     * @return parking result
     */
    boolean parking(Car car) {
        if (car && hasLocation() && !cars.containsKey(car.number)) {
            cars[car.number] = car
            return true
        }
        return false
    }

    boolean hasLocation() {
        return cars.size() < this.size
    }

    boolean hasCar(String carNumber) {
        return this.cars.keySet().any { it == carNumber }
    }

    int availableLocation() {
        return this.size - cars.size()
    }

    boolean exists(Car car) {
        return this.cars.values().any { it.number == car.number }
    }

    /**
     * Task #2
     * @param carNumber
     * @return car
     */
    Car pickup(String carNumber) {
        if (carNumber && cars[carNumber]) {
            Car car = cars[carNumber]
            cars.remove(carNumber)
            return car
        }
        return null
    }

}
